package com.eurobcoin.manager;

import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;

import com.eurobcoin.dao.mapper.StaticTextMapper;
import com.eurobcoin.dao.model.StaticText;
import com.eurobcoin.dao.model.StaticTextExample;
import com.eurobcoin.dao.model.StaticTextExample.Criteria;


public class StaticTextsManager { 

	private static final Logger logger = LoggerFactory.getLogger(StaticTextsManager.class);
	
	private DAOManager daoManager;
	
	public StaticTextsManager() {
		logger.info("Starting static texts manager.");
	}
	
	public void setDaoManager(DAOManager dao) {
		this.daoManager = dao;
	}
	
	public String getText(String label, Object... params) {
		Locale locale = LocaleContextHolder.getLocale();
		
		SqlSession sess = this.daoManager.openSession();
		
		StaticTextMapper mapper = sess.getMapper(StaticTextMapper.class);
		StaticTextExample example = new StaticTextExample();
		Criteria criteria = example.createCriteria();
		criteria.andTxt_langEqualTo(locale.getLanguage());
		criteria.andTxt_labelEqualTo(label);
		
		List<StaticText> list = mapper.selectByExample(example);
		
		String ret = null;
		String content = null;
		
		if(list.size() > 0)
			content = list.get(0).getTxt_content(); 
		
		if(content != null) {
			ret = MessageFormat.format(content, params); 
		}
		
		if(ret == null)
			ret = "";
		
		sess.close();
		
		return ret;
	}
	
}